
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2007 LME <webmaster@immd5.informatik.uni-erlangen.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/


	// DEFAULT initialization of a module [BEGIN]
unset($MCONF);
require_once('conf.php');
require_once($BACK_PATH.'init.php');
require_once($BACK_PATH.'template.php');

$LANG->includeLLFile('EXT:univis2web/mod1/locallang.xml');
require_once(PATH_t3lib.'class.t3lib_scbase.php');

$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
	// DEFAULT initialization of a module [END]



/**
 * Module 'Univis2Web' for the 'univis2web' extension.
 *
 * @author	LME <webmaster@immd5.informatik.uni-erlangen.de>
 * @package	TYPO3
 * @subpackage	tx_univis2web
 */
class  tx_univis2web_module1 extends t3lib_SCbase {
	var $pageinfo;

	/**
	 * Initializes the Module
	 * @return	void
	 */
	function init()	{
		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

		parent::init();

		/*
		if (t3lib_div::_GP('clear_all_cache'))	{
			$this->include_once[] = PATH_t3lib.'class.t3lib_tcemain.php';
		}
		*/
	}

	/**
	 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
	 *
	 * @return	void
	 */
	function menuConfig()	{
		global $LANG;
		$this->MOD_MENU = Array (
			'function' => Array (
				'1' => $LANG->getLL('function1'),
				'2' => $LANG->getLL('function2'),
				'3' => $LANG->getLL('function3'),
				'4' => $LANG->getLL('function4'),
				'5' => $LANG->getLL('function5'),
			)
		);
		parent::menuConfig();
	}

	/**
	 * Main function of the module. Write the content to $this->content
	 * If you chose "web" as main module, you will need to consider the $this->id parameter which will contain the uid-number of the page clicked in the page tree
	 *
	 * @return	[type]		...
	 */
	function main()	{
		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

		// Access check!
		// The page will show only if there is a valid page and if this page may be viewed by the user
		$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
		$access = is_array($this->pageinfo) ? 1 : 0;

		if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id) || ($GLOBALS["BE_USER"]->user['uid'] && !$this->id))	{

				// Draw the header.
			$this->doc = t3lib_div::makeInstance('mediumDoc');
			$this->doc->backPath = $BACK_PATH;
			$this->doc->form='<form action="" method="POST" enctype="multipart/form-data">';

				// JavaScript
			$this->doc->JScode = '
				<script language="javascript" type="text/javascript">
					script_ended = 0;
					function jumpToUrl(URL)	{
						document.location = URL;
					}
				</script>
			';
			$this->doc->postCode='
				<script language="javascript" type="text/javascript">
					script_ended = 1;
					if (top.fsMod) top.fsMod.recentIds["web"] = 0;
				</script>
			';

			$this->content.=$this->doc->startPage($LANG->getLL('title'));
			$this->content.=$this->doc->header($LANG->getLL('title'));
			$this->content.=$this->doc->spacer(5);

			// Render content:
			$this->moduleContent();

		} else {
				// If no access or if ID == zero

			$this->doc = t3lib_div::makeInstance('mediumDoc');
			$this->doc->backPath = $BACK_PATH;

			$this->content.=$this->doc->startPage($LANG->getLL('title'));
			$this->content.=$this->doc->header($LANG->getLL('title'));
			$this->content.=$this->doc->spacer(5);
			$this->content.=$this->doc->spacer(10);
			$this->content.="no access";
		}
	}

	/**
	 * Prints out the module HTML
	 *
	 * @return	void
	 */
	function printContent()	{

		$this->content.=$this->doc->endPage();
		echo $this->content;
	}

	/**
	 * Generates the module content
	 *
	 * @return	void
	 */
	function moduleContent() {
		$content="<div align=center><strong>".$GLOBALS["BE_USER"]->user['realName']."</strong> - Settings</div>";
		$content = '<table>'.$this->setVision().$this->uploadImage().
			'<tr><td colspan="2"><input type="submit" value="Save"></td></tr></table>';
		$this->content.=$this->doc->section($GLOBALS["BE_USER"]->user['realName'].'  Settings',$content,0,1);
	}

	function setVision()
	{
		if( !isset($_POST['vision']) || !isset($_POST['vision_en']) )
		{
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
				'tx_univis2typo3_vision, tx_univis2typo3_vision_en',	// SELECT
				'be_users',						// FROM
				'uid='.$GLOBALS["BE_USER"]->user['uid']			// WHERE
			);

			$row       = mysql_fetch_assoc($res);
			$vision    = $row['tx_univis2typo3_vision'];
			$vision_en = $row['tx_univis2typo3_vision_en'];

			$content .= '<tr><td>Vision (German):</td>'.
				'<td><input type="text" value="'.$vision.'" name="vision"></td></tr>'.
                                             '<tr><td>Vision (English):</td>'.
				'<td><input type="text" value="'.$vision_en.'" name="vision_en"></td></tr>';

		}
		else
		{
			$GLOBALS['TYPO3_DB']->exec_UPDATEquery(
				'be_users',   						  					  // TABLE
				'uid='.$GLOBALS["BE_USER"]->user['uid'],                  // WHERE
				array('tx_univis2typo3_vision'=>$_POST['vision'],'tx_univis2typo3_vision_en'=>$_POST['vision_en'] )                  // FIELDS
			);

			$content = "<tr><td colspan=2>Vision successful changed to: ".$_POST['vision']." and ".
                                     	$_POST['vision_en']."</td></tr>";
		}
		return $content;
	}
	
	function uploadImage()
	{
		if(!isset($_FILES['image']))
		{
			$content = '<tr><td>Image:</td>'.
				'<td><input type="file" name="image"></td></tr>';
		}
		else
		{
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('firstname, lastname',
				'`be_users` JOIN tx_u2t3_person ON `tx_univis2typo3_person_key` = `key`',
				'`be_users`.uid = '.$GLOBALS["BE_USER"]->user['uid']
			);
			$myPath = substr($this->doc->backPath,0,-6)."/fileadmin/Persons/";
			$row  = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
			$myPath .= $row['lastname'].$row['firstname']."/";
			// If path does not exist, create it!
			if( !file_exists($myPath) )
				mkdir($myPath);
			$dateityp = GetImageSize($_FILES['image']['tmp_name']);
			
// 			if($_FILES['image']['size'] < 1048576)
// 				return $content = "File too big!";

			if( $dateityp[2] != 0 || $dateityp[2] != 4 )
			{
				if ( move_uploaded_file($_FILES['image']['tmp_name'], $myPath.$_FILES['image']['name']) )
				{
					$GLOBALS['TYPO3_DB']->exec_UPDATEquery(
						'be_users',					// TABLE
						'uid='.$GLOBALS["BE_USER"]->user['uid'],	// WHERE
						array('tx_univis2typo3_image'=> "/fileadmin/Persons/".$row['lastname'].$row['firstname'].'/'.$_FILES['image']['name'])           // FIELDS
					);
					$content .= "<tr><td colspan=2>Image successful uploaded to: ".$myPath.$_FILES['image']['name']."</td></tr>";
				}
			}
		}
		return $content;
	}

	function getItems($key='')
	{
	
		$select         = "";
		$from           = "`sys_filemounts` JOIN `be_users` ON `sys_filemounts`.`uid` = `be_users`.`file_mountpoints`";
		$where          = "`be_users`.`uid` = '".$key."'";
		$groupby        = "";
		$orderby        = "";
		$limit          = "";
	
		$query          = "SELECT ".$select." FROM ".$from;
		if ($where)
			$query .= " WHERE ".$where;
		if ($groupby)
			$query .= " GROUP BY ".$groupby;
		if ($limit)
			$query .= " LIMIT ".$limit;
	
		$row = $this->performSQLQuery($query,'true');
		
	
		return $row;
	}

	function performSQLQuery($query='',$output='false')
	{
		$out = array();
		$result = mysql_query($query);
		if ($output == 'true')
		{
			if (!$result)
			{
				$message  = 'Invalid query: ' . mysql_error() . "\n";
				$message .= 'Whole query: ' . $query;
				die($message);
			}
			while ($row = mysql_fetch_assoc($result))
				$out[] = $row;
			return $out;
		}
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2web/mod1/index.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2web/mod1/index.php']);
}




// Make instance:
$SOBE = t3lib_div::makeInstance('tx_univis2web_module1');
$SOBE->init();

// Include files?
foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);

$SOBE->main();
$SOBE->printContent();

?>
